
$(function() {
	
	$("input[id='emp']:visible").autocomplete(Test.emp);
	
	_ck_default("input[id='emp']:visible", "input[id^='emp_t']:visible");
	
	$('#date').datepicker(_now_option);
	
	$('#customer').change(function() {
		var a = $(this).val();
		// Pay.supplier(a);
		
		var body = { };
		body.id = a;
		$.getJSON('contact.php?do=Customer.ajax_id', body, function(json) {
			$("#a_name").val(json.name);
			$("#a_fullname").val(json.fullname);
			$("#a_address").val(json.address);
			$("#a_web").val(json.url);
			
			/*
			var s = $("#name").val();
			if (s == null || s == '') {
				$("#name").val(json.code);
			}*/
			
			// $("#emp").val(json.emp);
		});
		
		var body = {};
		body.q = a;
		
		$.getJSON('contact.php?do=Customer.ajax_linkman', body, function(json) {	
			// alert($.toJSON(json));
			Common.linkman(json);
		});
	});
	
	$("#label_linkman_a").hide();
	
	_change("#address", "select[id^='address_t']:visible");

	$(":text").change(function() {
		cart_update_amount(this.name, $(this).val());
		cart_update_price(this.name, $(this).val());

		cart_refresh();
    });

	cart_refresh();	

	$('textarea').css("width", 400);
});

function cart_update_amount(name, value) {
	if (!/^amount_/.test(name)) {
		return false;
	}

	if (eval(value) < 0) {
		return false;
	}

	var cart = cookie_cart();

	var cc = name.split("_");
	var id = cc[1];

	var aa = new Array();
	for (var i = 0; i < cart.length; i++) {
		if (cart[i][0] == id) {
			cart[i][2] = eval(value);
		}

		aa.push(cart[i]);
	}

	$.cookie('cart', $.toJSON(aa));
}

function cart_update_price(name, value) {
	if (!/^price_/.test(name)) {
		return false;
	}

	if (eval(value) < 0) {
		return false;
	}

	var cart = cookie_cart();

	var cc = name.split("_");
	var id = cc[1];

	var aa = new Array();
	for (var i = 0; i < cart.length; i++) {
		if (cart[i][0] == id) {
			cart[i][1] = eval(value);
		}

		aa.push(cart[i]);
	}

	$.cookie('cart', $.toJSON(aa));
}

function cart_del(id) {
	var answer = confirm("是否要删除？")
	if (!answer) {
		return false;
	}

	var cart = cookie_cart();

	var aa = new Array();
	for (var i = 0; i < cart.length; i++) {
		if (cart[i][0] == id) {
			continue;
		}

		aa.push(cart[i]);
	}

	json = $.toJSON(aa);
	if (aa != null) {
		$("#cart").text("采购单有[" + aa.length + "]条明细");
	}
	$.cookie('cart', json);

	$("#row_" + id).remove();

	cart_refresh();	
}

function amount_add(id) {
	var o = $("#amount_" + id);

	var a = eval(o.val());
	a = a < 1 ? 1 : a + 1;
	o.val(a);

	cart_update_amount('amount_' + id, $('#amount_' + id).val());

	cart_refresh();
}

function amount_dec(id) {
	var o = $("#amount_" + id);
	var a = eval(o.val());
	if (a > 1) {
		o.val(a - 1);
	}

	cart_update_amount('amount_' + id, $('#amount_' + id).val());

	cart_refresh();
}

function cart_refresh() {
	var a = $(":text");

	var t = 0, id = 0;
	var len = a.length;
	for (var i = 0; i < len; i++) {
		var name = a[i].name;
		if (/^price_/.test(name)) {
			id = name.substring(6);
			cart_update(name, id, 1);
		}

		if (/^amount_/.test(name)) {
			id = name.substring(7);
			cart_update(name, id, 2);
			
			t += eval($("#price_" + id).val()) * eval($("#amount_" + id).val());
		}
	}	

	var s = "总金额：" + fmoney(t, 2);
	
	$("#total").text(s);

	return false;
}

function cart_update(name, id, k) {
	var cart = cookie_cart();
		
	var len = cart.length;
	for (var i = 0; i < len; i++) {
		if (cart[i][0] == id && cart[i][k] != null) {
			$("#" + name).val(cart[i][k]);
			return true;
		}
	}
}
